<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * App\Models\Cash
 *
 * @property int $id
 * @property int $user_id 前台用户id
 * @property float $money 提现申请的金额
 * @property int $status 0-提现未处理  1-提现已处理
 * @property int $get_cash_status 到款状态 0-未到款  1-已到款
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @property string|null $phone 手机号
 * @property string|null $alipay_account 支付宝账号
 * @property string|null $true_name 真是姓名
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Cash whereAlipayAccount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Cash whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Cash whereGetCashStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Cash whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Cash whereMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Cash wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Cash whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Cash whereTrueName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Cash whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Cash whereUserId($value)
 * @mixin \Eloquent
 */
class Cash extends Model
{
    protected $table   = 'cash';
    protected $fillable = ['user_id','created_at','updated_at','money','alipay_account','phone','true_name'];
    public static  $error ;

    /**提现列表数据
     * @return array
     */
    public function cash_list()
    {
         $config_sercie_money = SystemConfig::getConfValByConfKey('cash_service_charge');
         $service_charge  = empty($config_sercie_money) ? 0 : $config_sercie_money['conf_val'] ;
         $data = $this -> newQuery()
            -> select('user.user_name','cash.*')
            -> leftJoin('user','user.id','=','cash.user_id')
            -> get()
            -> toArray();
         foreach($data as &$v)
         {
            $v['true_money'] = $v['money'] * (100 - $service_charge) / 100;
         }

         return $data;
    }

    /**
     * 提现操作
     */
    public function cash($cash_id)
    {
        $cash_model = self::find($cash_id);
        if($cash_model -> status == 1){
            return true;
        }
        $cash_model -> status = 1;
        if($cash_model -> save()){
            return true;
        }

        return false;
    }


    /**api调用的前端提现操作
     * [
     *   money    =>  12,
     *   user_id  =>  1,
     * ]
     * @param $data array 前端发送过来的数据
     */
    public function api_cash($data)
    {
        $cash_model = self::query()
            ->where('user_id','=',$data['hotel_open_id'])
            ->orderBy('created_at','desc')
            ->first();
        if(empty($hotel_owner_model = HotelOwnerApply::is_valid_hotel_owner($data['hotel_open_id']))){
            self::$error = '该用户未入驻酒店，非法操作!';
            return false;
        }

        //检查提现的金额是否大于所剩的佣金
        if($data['money'] > $hotel_owner_model->money){
            self::$error = '余额不足无法提现';
            return false;
        }

//        if($cash_model){
//            //提现需要间隔一周
//            if(time() - strtotime($cash_model->created_at) < 7*24*3600){
//                self::$error = '提现时间间隔必须大于一周';
//                return false;
//            }
//        }

        //开始提现操作
        DB::beginTransaction();
        try{
            if(self::create($data)){
                //减去用户的余额
                $hotel_owner_model -> money -= $data['money'];
                if(!$hotel_owner_model->save()){
                    throw new \Exception('用户余额提现失败');
                }
                DB::commit();
                return true;
            }
            throw new \Exception('提现失败');
        }catch(\Exception $e){
            self::$error = $e -> getMessage();
            DB::rollBack();
            return false;
        }
    }

}
